package com.example.medical_delivery_system.service.impl;

import com.example.medical_delivery_system.common.ServiceResultEnum;
import com.example.medical_delivery_system.dao.AdminOrderMapper;
import com.example.medical_delivery_system.entity.Order;
import com.example.medical_delivery_system.service.AdminOrderService;
import com.example.medical_delivery_system.util.PageQueryUtil;
import com.example.medical_delivery_system.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Param：20201003013
 * Author：李镇荣
 * Data：2022/12/21
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    private AdminOrderMapper adminOrderMapper;


    @Override
    public PageResult getOrderPage(PageQueryUtil pageUtil) {
        List<Order> orders = adminOrderMapper.findOrderList(pageUtil);
        int total = adminOrderMapper.getTotalOrders(pageUtil);
        PageResult pageResult = new PageResult(orders, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveOrder(Order order) {
        if (adminOrderMapper.insertSelective(order) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateOrder(Order order) {
        Order temp = adminOrderMapper.selectByPrimaryKey(order.getOrderId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        temp.setUserId(order.getUserId());
        temp.setDeliveryId(order.getDeliveryId());
        temp.setAddress(order.getAddress());
        temp.setTotalPrice(order.getTotalPrice());
        temp.setTime(order.getTime());
        temp.setStatus(order.getStatus());
        if (adminOrderMapper.updateByPrimaryKeySelective(temp) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public Order getOrderById(Long id) {
        return adminOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean deleteBatch(Long[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除数据
        return adminOrderMapper.deleteBatch(ids) > 0;
    }
}
